package com.jar.exercise.多线程.AQS.LockSupport.线程交替;

public class SynDemo {
    public static int num = 1;

    public static void main(String[] args) throws InterruptedException {
        Thread A = new Thread(new MyThread(0), "A");
        Thread B = new Thread(new MyThread(1), "B");
        Thread C = new Thread(new MyThread(2), "C");
        A.start();
        B.start();
        C.start();
    }

}

class MyThread implements Runnable {
    private static Object lock = new Object();
    public int num;
    public static int count = 0;

    public MyThread(int num) {
        this.num = num;
    }

    @Override
    public void run() {

        while (true) {
            synchronized (lock) {
                try {
                    if (count > 100) {
                        lock.notifyAll();
                        break;
                    }
                    while (count % 3 == num) {
                        System.out.println(Thread.currentThread().getName() + "::" + count);
                        count++;
                        lock.notifyAll();
                    }
                    lock.wait();


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}